package cn.dhnep.demo.service.impl;

import cn.dhnep.demo.mapper.EnvironmentalGuardMapper;
import cn.dhnep.demo.pojo.Promote;
import cn.dhnep.demo.pojo.Result;
import cn.dhnep.demo.pojo.User;
import cn.dhnep.demo.pojo.entity.SfOrder;
import cn.dhnep.demo.service.EnvironmentalGuardService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @Author 邱文彬
 * @Date 2021/11/12 15:23
 * @Version 1.0
 */


@Service
public class EnvironmentalGuardServiceImpl implements EnvironmentalGuardService {


    @Autowired
    private EnvironmentalGuardMapper environmentalGuardMapper;

    @Override
    public Result getEnvironmentalGuardAllByOpenId(String openId,long page, long record) {
        User allUser = environmentalGuardMapper.getAllUser(openId);

        if(allUser == null){
            return  new Result(10001,"openId不存在",null);
        }


        List<Promote> list = new ArrayList<>();

        List<User> osdn25R_shT_sitml7BOTrLTith4 = environmentalGuardMapper.getNumberOfSuborgetLowerLevelDetailsByOpenIddinatesByOpenId(openId);
        int num1 = 0;
        int num2 = 0;
        int num3 = 1;
        for (User user : osdn25R_shT_sitml7BOTrLTith4) {

            if(num3 <= page * record && num3 > (page -1) * record ){
                num3++;
                List<Promote> list2 = new ArrayList<>();
                String avatarUrl = user.getAvatarUrl();
                Date date = user.getCreateTime();
                String name = user.getNickName();

                Integer integer = osdn25R_shT_sitml7BOTrLTith4.size();
                Promote promote = new Promote();
                promote.setDate(date);
                promote.setUrl(avatarUrl);
                promote.setName(name);
                promote.setNum1(integer);
                List<User> list1 = environmentalGuardMapper.getNumberOfSuborgetLowerLevelDetailsByOpenIddinatesByOpenId(user.getOpenId());
                if(list1.size() == 0){
                    list2 = null;
                    promote.setList(list2);
                    list.add(num1,promote);
                    num1++;
                    num2 = 0;
                }else{
                    for (User user1 : list1) {
                        String avatarUrl1 = user1.getAvatarUrl();
                        Date date1 = user1.getCreateTime();
                        String name1 = user1.getNickName();
                        Promote promote2 = new Promote();
                        promote2.setDate(date1);
                        promote2.setUrl(avatarUrl1);
                        promote2.setName(name1);
                        list2.add(num2,promote2);
                        num2++;
                    }
                    promote.setList(list2);
                    list.add(num1,promote);
                    num1++;
                    num2 = 0;



            } }else {
                if(page != 1){
                    num3++;
                }
            }


        }


            return new Result(0,"操作成功",list);


    }
}
